﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo;

namespace www.serviciipeweb.ro.CDC
{
    public class ExceptionServer : Exception
    {
        public ExceptionServer(eServerCDC Cause)
            : base(" Server problem  : " + Cause.ToString())
        {
        }
    }
    public class FacadeCDC
    {
        public string ServerName ;
        public string DatabaseName;
        public string TableName;
        private Server ServerToConnect
        {
            get
            {
                Server s = new Server(ServerName);
                s.InitializeConnection();
                eServerCDC e;
                if (!s.EnableCDC())
                {
                    if(!s.IsCDCEnabled(out e))
                        throw new ExceptionServer(e);
                }
                return s;
            }
        }
        public void EnableCDC()
        {

            ServerToConnect.Databases[DatabaseName].Tables[TableName].EnableCDCRecursive();

        }
        public TableChangesCollection Changes()
        {
            Table t =ServerToConnect.Databases[DatabaseName].Tables[TableName];
            if(!t.IsCDCEnabled())
                return null;

            return t.TableCDC().AllChanges();
        }

    }
}
